<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Three框架</title>
    <script src="../thingjs/js/three.js" data-ke-src="../thingjs/js/three.js"></script>
    <style type="text/css">
        div#canvas-frame {
            border: none;
            cursor: pointer;
            width: 100%;
            height: 600px;
            background-color: #EEEEEE;
        }

    </style>
</head>

<body onload="threeStart();">
<div id="canvas-frame"></div>
</body>

<script>

    //渲染
    var renderer;
    function initThree() {
        width = document.getElementById('canvas-frame').clientWidth;
        height = document.getElementById('canvas-frame').clientHeight;
        renderer = new THREE.WebGLRenderer({
           antialias : true
        });
        renderer.setSize(width, height);
        document.getElementById('canvas-frame').appendChild(renderer.domElement);
        renderer.setClearColor(0xFFFFFF, 1.0);
    }

    //相机
    var camera;
    function initCamera() {
        camera = new THREE.PerspectiveCamera(45, width / height, 1, 10000);
        camera.position.x = 0;
        camera.position.y = 1000;
        camera.position.z = 0;
        camera.up.x = 0;
        camera.up.y = 0;
        camera.up.z = 1;
        camera.lookAt({
           x : 0,
           y : 0,
           z : 0
        });
    }

    //场景
    var scene;
    function initScene() {
        scene = new THREE.Scene();
    }

    var light;
    function initLight() {
        light = new THREE.DirectionalLight(0xFF0000, 1.0, 0);
        light.position.set(100, 100, 200);
        scene.add(light);
    }

    //构建 心法：使用相机将场景渲染到页面上去
    var cube;
    function initObject() {
        //构建几何体
        var geometry = new THREE.Geometry();
        //定义线的材质
        var material = new THREE.LineBasicMaterial({
           vertexColors : true
        });

        //线条两个端点的颜色
        var color1 = new THREE.Color(0x444444);
        var color2 = new THREE.Color(0x4B0082);

        //定义2个顶点的位置，并放到geometry中
        var p1 = new THREE.Vector3(-100, 0, 100);
        var p2 = new THREE.Vector3(100, 0, -100);
        //为2个顶点，设置不同的颜色
        geometry.vertices.push(p1);
        geometry.vertices.push(p2);
        geometry.colors.push(color1, color2);
        //定义一条线 给线加一种材质
        var line = new THREE.Line(geometry, material, THREE.LinePieces);
        scene.add(line);//将线段加入到场景中
    }

    //初始化加载
    function threeStart() {
        initThree();
        initCamera();
        initScene()
        initLight();
        initObject();
        renderer.clear();
        renderer.render(scene, camera);//渲染场景
    }



</script>

</html>